<template>
  <div class="detection-table">
    <!-- 表头 -->
    <div class="table-header">
      <div class="header-cell">检测编号</div>
      <div class="header-cell">检测样品</div>
      <div class="header-cell">检测结果</div>
      <div class="header-cell">被检单位</div>
      <div class="header-cell">检测时间</div>
    </div>

    <!-- 表格内容 -->
    <div class="table-body">
      <div
          v-for="(item, index) in data"
          :key="index"
          class="table-row"
      >
        <div class="table-cell">{{ item.id  }}</div>
        <div class="table-cell">{{ item.sample  }}</div>
        <div class="table-cell" :class="getResultClass(item.result)">
          {{ item.result  }}
        </div>
        <div class="table-cell">{{ item.unit  }}</div>
        <div class="table-cell time-cell">{{ formatTime(item.timestamp)  }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetectionTable',
  props: {
    data: {
      type: Array,
      required: true,
      default: () => []
    }
  },
  setup(props) {
    // 格式化时间
    const formatTime = (timestamp) => {
      const date = new Date(timestamp)
      return `${date.getHours().toString().padStart(2,  '0')}:${date.getMinutes().toString().padStart(2,  '0')}:${date.getSeconds().toString().padStart(2,  '0')}`
    }

    // 结果样式类
    const getResultClass = (result) => {
      return {
        'result-pass': result === '合格',
        'result-fail': result === '不合格',
        'result-pending': result === '检测中'
      }
    }

    return {
      getResultClass,
      formatTime
    }
  }
}
</script>

<style scoped>
.detection-table {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  font-size: 13px;
  background: rgba(13, 42, 67, 0.3);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.table-header {
  display: flex;
  background: linear-gradient(90deg, rgba(11, 93, 232, 0.5), rgba(79, 216, 222, 0.3));
  padding: 12px 5px;
  border-bottom: 1px solid rgba(79, 216, 222, 0.2);
}

.header-cell {
  flex: 1;
  text-align: center;
  font-weight: 500;
  color: #4fd8de;
  padding: 0 5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.table-body {
  flex: 1;
  overflow-y: auto;
}

.table-row {
  display: flex;
  padding: 12px 5px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  transition: all 0.4s ease;
  position: relative;
}

.table-row:hover {
  background: rgba(255, 255, 255, 0.05);
}

.table-cell {
  flex: 1;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 5px;
  color: rgba(255, 255, 255, 0.85);
}

.time-cell {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
}

/* 高亮行效果 */
.highlight-row {
  background: rgba(79, 216, 222, 0.15);
  box-shadow: 0 0 10px rgba(79, 216, 222, 0.2);
}

/* 新增数据动画 */
.new-row {
  animation: pulse 1.5s ease;
}

@keyframes pulse {
  0% { background-color: rgba(79, 216, 222, 0.3); }
  100% { background-color: transparent; }
}

/* 检测结果样式 */
.result-pass {
  color: #52c41a;
  font-weight: bold;
  position: relative;
}

.result-pass::before {
  content: '✓';
  margin-right: 4px;
}

.result-fail {
  color: #ff4d4f;
  font-weight: bold;
  position: relative;
}

.result-fail::before {
  content: '✕';
  margin-right: 4px;
}

.result-pending {
  color: #faad14;
  font-weight: bold;
  position: relative;
}

.result-pending::before {
  content: '…';
  margin-right: 4px;
}

/* 自定义滚动条 */
.table-body::-webkit-scrollbar {
  width: 6px;
}

.table-body::-webkit-scrollbar-track {
  background: rgba(13, 42, 67, 0.3);
  border-radius: 3px;
}

.table-body::-webkit-scrollbar-thumb {
  background: linear-gradient(to bottom, #4fd8de, #0b5de8);
  border-radius: 3px;
}
</style>